package com.timothy.android;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.timothy.answer.BaseAnswer;
import com.timothy.service.MessageSender;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BaseQuestionActivity extends BaseProcessActivity {
	private static final String LOG_TAG = "BaseQuestionActivity";
	public LayoutSetting setting = null;
	
	protected int countDownTime = -1;
	protected CustomToast ct = null;
	
    public void appendTitle(String append){
    	this.setTitle(getTitle()+append);
	}
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//load screen size
		Log.i(LOG_TAG,"Start Load Screen Setting...");
		setting = new LayoutSetting(screenSize,screenHeight,screenHeight);
		
		//set orientation
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	//send answers to server
	public void sendAnswer(BaseAnswer baseAnswer){
		if( !MessageSender.sendAnswer(baseAnswer) ) {
			ShowDialog("Can not connect to pc, please confirm PC is running!");
		}
	}
	
	//倒计时内部类
	class MyCount extends CountDownTimer { 
		List<View> viewList = new ArrayList<View>();
		
		public MyCount(long millisInFuture, long countDownInterval,List<View> btnList) {   
			super(millisInFuture, countDownInterval);   
			this.viewList = btnList;
		}   
		@Override   
		public void onFinish() {   
			for(int i=0;i<viewList.size();i++){
				View btn = viewList.get(i);
				btn.setEnabled(false);
			}
		}   
		@Override   
		public void onTick(long millisUntilFinished) {
			
		}
	}
	
	class CustomToast {
		private int showCount = 0;
		private Toast toast = null;
		private Context context;
		private Handler handler = null;
		
		private Runnable toastThread = new Runnable() {
			public void run() {
				// 递增的count明显地表明是不断运行新的Toast.show()的结果。
				toast.setText(String.valueOf(showCount--) + " S");
				toast.show();
				// 3.3秒后再度重启，设为4s的话将会看到Toast是断断续续地显示着的。
				handler.postDelayed(toastThread, 1000);
			}
		};
		public CustomToast(Context context,int time) {
			this.context = context;
			showCount = time;
			handler = new Handler(this.context.getMainLooper());
			toast = Toast.makeText(this.context, "", Toast.LENGTH_LONG);
		}
		public void setText(String text) {
			toast.setText(text);
		}
		public void showToast(final long length) {
			handler.post(toastThread);
			Thread timeThread = new Thread() {
				public void run() {
					try {
						Thread.sleep(length);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					CustomToast.this.stopToast();
				}
			};
			timeThread.start();
		}
		public void stopToast() {
			// 删除Handler队列中的仍处理等待的消息元素删除
			handler.removeCallbacks(toastThread);
			// 撤掉仍在显示的Toast
			toast.cancel();
		}
	}	
	
	protected void startCountDown(int countDownTime,List<View> viewList){
		if(countDownTime!= -1&&countDownTime>0){
			ct = new CustomToast(this.getApplicationContext(),countDownTime);
			ct.showToast(countDownTime * 1000);
			MyCount mc = new MyCount(countDownTime * 1000,1000,viewList);
			mc.start();
		}
	}
	
}
